//
//  JMSetNicknameViewController.m
//  jiMuOperate
//
//  Created by 翁仪 on 16/5/4.
//  Copyright © 2016年 jinXinFengLi. All rights reserved.
//

#import "JMNickNameVController.h"
#import "JMOperate.pch"
#import "JMCellView.h"
#import "JMCheckNameInfo.h"
#import "JMHomeVController.h"
@interface JMNickNameVController ()<UITextFieldDelegate>

@end

@implementation JMNickNameVController
{
    UITextField* _nickNameField;
    
    UILabel* remindView;
    UIButton *setBut;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationItem.title = @"设置昵称";
    [self setupOther];
    [self addRemindView];
    [self addSetBut];

}
-(void)setupOther{
    
    self.tableView.scrollEnabled = NO;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    self.tableView.tableHeaderView = view;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BACKCOLOR;
    UITapGestureRecognizer *singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapFrom)];
    singleRecognizer.numberOfTapsRequired = 1;
    [self.tableView addGestureRecognizer:singleRecognizer];
}
- (void)handleSingleTapFrom
{
    [_nickNameField resignFirstResponder];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"AuthenticationCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    //添加cell里面的控件
    UILabel *label = [ARC createLabelWithFrame:CGRectMake(10, 0, 100, CELLHIGHT) Font:14 Text:@"昵称"];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor blackColor];
    [cell.contentView addSubview:label];
    if (!_nickNameField) {
        
        _nickNameField = [self addNickNameFieldWithFrame:CGRectMake(label.frame.size.width, 0, SCREEN_WIDTH-label.frame.size.width, CELLHIGHT)];
        
        [cell.contentView  addSubview:_nickNameField];
    }


    return [JMCellView setupCell:cell HideLine:YES];

}
- (JMTextField *)addNickNameFieldWithFrame:(CGRect)frame
{
    //用户名
    JMTextField *textField = [JMTextField createTextFieldWithFrame:frame
                                                              Font:FONTSIZE
                                                       Placeholder:@"长度2-10个字符之间"
                                                   SecureTextEntry:NO
                                                         TextColor:[UIColor blackColor]];
    textField.keyboardType =  UIKeyboardTypeDefault;
    textField.delegate = self;
    textField.frame =  [ToolViewAndData MyAutoLayout:textField];
    return  textField;
    
}
-(void)addRemindView{
    
    UIView *labelView = [ARC createViewWithFrame:CGRectMake(0, 64, 320, 50)];
    int labelFont = 13;
    UILabel *label1 = [ARC createLabelWithFrame:CGRectMake(10, 0, 320, 20) Font:labelFont Text:@"温馨提示:"];
    label1.textColor = RGB(150, 150, 150);
    label1.textAlignment = NSTextAlignmentLeft;
    UILabel *label2 = [ARC createLabelWithFrame:CGRectMake(10, 20, 320, 20) Font:labelFont Text:@"1，昵称设置后不可更改"];
    label2.textColor = RGB(150, 150, 150);
    label2.textAlignment = NSTextAlignmentLeft;
   
    
    [labelView addSubview:label1];
    [labelView addSubview:label2];
    [self.view addSubview:labelView];
}
-(void)addSetBut{
    
    CGFloat btnY = 200;
    CGFloat btnH = 40;
    
    setBut = [ARC createButtonWithFrame:CGRectMake(30,btnY, 260, btnH)
                                Target:self
                                   Sel:@selector(setBtnClick:)
                                 Title:@"设置"];
    
    [setBut setBackgroundImage:[UIImage imageNamed:@"logoin_btn"] forState:UIControlStateNormal];
    setBut.frame = [ToolViewAndData MyAutoLayout:setBut];
    [setBut.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    setBut.tintColor=[UIColor whiteColor];
    [self.view addSubview:setBut];
    
}

-(void)setBtnClick:(UIButton*)btn

{
    if (_nickNameField.text.length<2||_nickNameField.text.length>10) {
        SHOWALERT(@"昵称长度不在设置范围内");
        //1.获取NSUserDefaults对象
        NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
        //保存数据(如果设置数据之后没有同步, 会在将来某一时间点自动将数据保存到Preferences文件夹下面)
        [defaults setObject:_nickNameField.text forKey:@"NickName"];
        //3.强制让数据立刻保存
        NSLog(@"%@",_nickNameField.text);
        [defaults synchronize];
        return;
        
    }
    NSString *token = [UserEntity shardUserInfo].access_token;
    [self requestSetNickNameWithNickName:_nickNameField.text Token:token];
    NSLog(@"fdg:%@",_nickNameField.text);
    NSLog(@"token:%@",token);

}

- (void)requestSetNickNameWithNickName:(NSString *)NickName Token:(NSString *)token
{
    NSString *url = [NSString stringWithFormat:@"%@%@&Token=%@",JMURLSetNickName,NickName,token];
    NSArray *arrayR = @[@"",url,@"GET"];
    [Request startRequestWithArray:arrayR FinishedBlock:^(NSData *data) {
        NSDictionary *dict = [JMData dictAndTestData:data];
        JMCheckNameInfo *checkName = [JMCheckNameInfo objectWithKeyValues:dict];
        NSLog(@"dgfd%@",dict);
        if ([checkName.State intValue]>0) {
            JMHomeVController *homeVc = [[JMHomeVController alloc]init];
            [self.navigationController pushViewController:homeVc animated:YES];
        }else{
            SHOWALERT(checkName.Msg);
        }
        
    } FailBlock:^(NSString *string) {
        SHOWALERT(@"服务器繁忙，请梢后重试");
    }];
}
@end
